angular.module('myApp', ['ui.bootstrap','ui.router'])

    .controller('RegisterCtrl',  [ '$http','$scope','$state', function ($http,$scope,$state) {

    $scope.submit=function () {
        var url='/accounts/register/';
        var config={};

        console.log("post:"+JSON.stringify($scope.user));
        $http.post(url,$scope.user,config).then(function successCallback(response) {
            var status_code=response.data.status_code;
            switch (status_code){
                case 200:
                    $scope.errors={};
                    $state.go("registerSuccess");
                    break;
                case 422:
                    $scope.errors=response.data.errors;
                    break;
            }
        }, function errorCallback(response) {
            console.log(JSON.stringify(response.data));
        });
 
    };

}]).config(['$stateProvider', '$urlRouterProvider',
    function ($stateProvider,   $urlRouterProvider) {
    var registerState = {
        name: 'register',
        url: '/register',
        templateUrl: '/static/templates/register.tpl.html',
        controller: 'RegisterCtrl'
    };

    var registerDoneState = {
        name: 'registerSuccess',
        url: '/register/success/',
        template: '<p>注册成功！<a href="/accounts/#/login">点击这里登陆</a></p>',



    };

    $stateProvider.state(registerState);
    $stateProvider.state(registerDoneState);
        $urlRouterProvider .otherwise('/register');
}])
    ;

